﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:Chance
    /// </summary>
    public partial class ChanceDAL
    {
        public ChanceDAL()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Chance");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Chance");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.Chance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Chance(");
            strSql.Append("ChanceOperator,ChanceValue,ChanceMouldID,TypeID,MaxNum,MinNum)");
            strSql.Append(" values (");
            strSql.Append("@ChanceOperator,@ChanceValue,@ChanceMouldID,@TypeID,0,0)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ChanceOperator", SqlDbType.VarChar,10),
					new SqlParameter("@ChanceValue", SqlDbType.Int),
					new SqlParameter("@ChanceMouldID", SqlDbType.Int),
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
            parameters[0].Value = model.ChanceOperator;
            parameters[1].Value = model.ChanceValue;
            parameters[2].Value = model.ChanceMouldID;
            parameters[3].Value = model.TypeID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 从chancemould中更新
        /// </summary>
        /// <param name="newName"></param>
        /// <param name="oldName"></param>
        /// <param name="sortNum"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        //public bool UpdateNameAndSortNum(string newName, string oldName, int sortNum, string typeName)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("update Chance set ");
        //    strSql.Append("ChanceName=@newName,SortNum=@SortNum,TypeName=@TypeName ");
        //    strSql.Append(" where ChanceName=@oldName and ChanceEventType=0");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@newName", SqlDbType.VarChar,50),
        //            new SqlParameter("@oldName", SqlDbType.VarChar,50),
        //            new SqlParameter("@SortNum", SqlDbType.Int),
        //            new SqlParameter("@TypeName", SqlDbType.VarChar,20) };
        //    parameters[0].Value = newName;
        //    parameters[1].Value = oldName;
        //    parameters[2].Value = sortNum;
        //    parameters[3].Value = typeName;
        //    try
        //    {
        //        DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.WriteLog(ex);
        //        return false;
        //    }
        //}

        public bool UpdateChanceValue(GtsServer.Model.Chance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Chance set ");
            strSql.Append("ChanceValue=@ChanceValue,MaxNum=@MaxNum,MinNum=@MinNum ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ChanceValue", SqlDbType.Int),
					new SqlParameter("@MaxNum",SqlDbType.Int,4),
					new SqlParameter("@MinNum",SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.ChanceValue;
            parameters[1].Value = model.MaxNum;
            parameters[2].Value = model.MinNum;
            parameters[3].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 冲chancemould中删除
        /// </summary>
        /// <param name="ChanceName"></param>
        /// <returns></returns>
        public bool DeleteByChanceMouldName(int mouldID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Chance ");
            strSql.Append(" where ChanceMouldID=@mouldID ");
            SqlParameter[] parameters = {
					new SqlParameter("@mouldID", SqlDbType.Int)};
            parameters[0].Value = mouldID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByTypeID(int typeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Chance ");
            strSql.Append(" where typeID=@typeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@typeID", SqlDbType.Int,4)};
            parameters[0].Value = typeID;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
            return false;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select c.ID,c.TypeID,c.ChanceOperator,c.ChanceValue,c.ChanceMouldID,c.MaxNum,c.MinNum,cm.TypeName,cm.MouldName ChanceName ");
            strSql.Append(" FROM Chance  c left join dbo.ChanceMould cm on c.ChanceMouldID=cm.ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " order by cm.SortNum,c.typeID");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,ChanceTypeID,ChanceOperator,ChanceValue,ChanceName,MaxNum,MinNum ");
            strSql.Append(" FROM Chance ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Chance";
            parameters[1].Value = "ChanceTypeID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

